package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.Global;
import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.CachedCommandUtil;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import com.hzhr.iot.collect.service.helper.Device_CollectDataServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_CollectDeviceServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_IotDeviceServiceHelper;
import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.collectData.CollectDataPostModel;
import com.hzhr.iot.device.sdk.model.collectDevice_statusInfo.CollectDevice_StatusInfoPutModel;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.sdk.service.Device_CollectDataService;
import com.hzhr.iot.device.sdk.service.Device_CollectDevice_StatusInfoService;
import com.hzhr.iot.device.sdk.service.Device_IotDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * 集中器 获取集抄数据
 * @author 48310
 *
 */
@Component
public class MessageProcessor74 implements MessageProcessor {
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	Device_IotDeviceService device_iotDeviceService;
	@Autowired
	Device_IotDeviceServiceHelper device_iotDeviceServiceHelper;
	@Autowired
	Device_CollectDeviceServiceHelper device_collectDeviceServiceHelper;
	@Autowired
	Device_CollectDevice_StatusInfoService device_collectDevice_statusInfoService;
	@Autowired
	Device_CollectDataService device_collectDataService;
	@Autowired
	Device_CollectDataServiceHelper device_collectDataServiceHelper;
	@Autowired
	Global global;
	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		String ccrCode = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_CONCENTRATOR_NO));

		Object respMeterNo = parsedMessageObj.get(MessageFieldKeyEnum.RESP_METER_NO);
		List<String> meterCodeList = new ArrayList<>();
		if (respMeterNo != null) {
			meterCodeList = (List<String>) respMeterNo;
		}
		Object respJichaoriqi = parsedMessageObj.get(MessageFieldKeyEnum.RESP_JICHAORIQI);
		List<String> currentDateList = new ArrayList<>();
		if (respJichaoriqi != null) {
			currentDateList = (List<String>) respJichaoriqi;
		}
		Object respJichaoshijian = parsedMessageObj.get(MessageFieldKeyEnum.RESP_JICHAOSHIJIAN);
		List<String> currentTimeList = new ArrayList<>();
		if (respJichaoriqi != null) {
			currentTimeList = (List<String>) respJichaoshijian;
		}
		Object respBiaojuzhishuZhengshu = parsedMessageObj.get(MessageFieldKeyEnum.RESP_BIAOJUZHISHU_ZHENGSHU);
		List<String> currentValueIntegerPartList = new ArrayList<>();
		if (respBiaojuzhishuZhengshu != null) {
			currentValueIntegerPartList = (List<String>) respBiaojuzhishuZhengshu;
		}
		Object respBiaojuzhishuXiaoshu = parsedMessageObj.get(MessageFieldKeyEnum.RESP_BIAOJUZHISHU_XIAOSHU);
		List<String> currentValueDecimalPartList = new ArrayList<>();
		if (respBiaojuzhishuXiaoshu != null) {
			currentValueDecimalPartList = (List<String>) respBiaojuzhishuXiaoshu;
		}
		Object respValveStatus = parsedMessageObj.get(MessageFieldKeyEnum.RESP_VALVE_STATUS);
		List<Integer> valveStatusList = new ArrayList<>();
		if (respValveStatus != null) {
			valveStatusList = (List<Integer>) respValveStatus;
		}
		Object respCollectStatus = parsedMessageObj.get(MessageFieldKeyEnum.RESP_COLLECT_STATUS);
		List<Integer> collectStatusList = new ArrayList<>();
		if (respCollectStatus != null) {
			collectStatusList = (List<Integer>) respCollectStatus;
		}

		for (int i = 0; i < meterCodeList.size(); i++) {
			String meterCode  = meterCodeList.get(i);
			String currentDate = currentDateList.get(i);
			String currentTime = currentTimeList.get(i);
			String currentValueIntegerPart = currentValueIntegerPartList.get(i);
			String currentValueDecimalPart = currentValueDecimalPartList.get(i);
			Integer valveStatus = valveStatusList.get(i);
			Integer collectStatus = collectStatusList.get(i);

			if (!StringUtils.isEmpty(ccrCode) && !StringUtils.isEmpty(meterCode)
					&& !StringUtils.isEmpty(currentDate) && !StringUtils.isEmpty(currentTime) &&
					!StringUtils.isEmpty(currentValueIntegerPart) && !StringUtils.isEmpty(currentValueDecimalPart)
					&& collectStatus != null && collectStatus == 1 ) {
				IotDeviceModel ccr = device_iotDeviceServiceHelper.getByCode(ccrCode);
				if (ccr != null) {
					Integer ccrId = ccr.getId();
					Integer meterId = device_collectDeviceServiceHelper.getIdByIotDeviceIdAndCode(ccrId, meterCode);

					if (meterId != null) {
						Date currentTm = HzhrDateUtil.strToDate(currentDate + currentTime, "yyyyMMddHHmmss");
						int count = device_collectDataServiceHelper.getCountByIotDeviceIdAndCollectDeviceIdAndCurrentTm(ccrId, meterId, currentTm);
						if (count == 0) {//如果采集数据不存在
							//记录采集数据
							CollectDataPostModel collectDataPostModel = new CollectDataPostModel();
							collectDataPostModel.setCollectDeviceId(meterId);
							collectDataPostModel.setIotDeviceId(ccrId);

							collectDataPostModel.setCurrentTm(currentTm.getTime());
							Double currentValue = new BigDecimal(currentValueIntegerPart + "." + currentValueDecimalPart).doubleValue();
							collectDataPostModel.setCurrentValue(currentValue);
							if (valveStatus == 0) {
								collectDataPostModel.setValveStatusCode("2");
							}
							if (valveStatus == 1) {
								collectDataPostModel.setValveStatusCode("1");
							}
							if (valveStatus == 2) {
								collectDataPostModel.setValveStatusCode("3");
							}
							Map<String, Object> query = new HashMap<>();
							device_collectDataService.post(global.initGlobalHeader(), query, collectDataPostModel);

							//更新设备状态信息
							CollectDevice_StatusInfoPutModel collectDevice_statusInfoPutModel = new CollectDevice_StatusInfoPutModel();
							collectDevice_statusInfoPutModel.setLastCollectTime(currentTm.getTime());
							collectDevice_statusInfoPutModel.setCurrentReading(currentValue);
							if (valveStatus == 0) {
								collectDevice_statusInfoPutModel.setCurrentValveStatusCode("2");
							}
							if (valveStatus == 1) {
								collectDevice_statusInfoPutModel.setCurrentValveStatusCode("1");
							}
							if (valveStatus == 2) {
								collectDevice_statusInfoPutModel.setCurrentValveStatusCode("3");
							}
							query = new HashMap<>();
							device_collectDevice_statusInfoService.putByCollectDeviceId(global.initGlobalHeader(), meterId, query, collectDevice_statusInfoPutModel);
						}
					}
				}
			}
		}

		return null;
	}
}
